package cn.acewill.yxd.yunxundian.ui.user;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.componet.NetWorkStateReceiver;
import cn.acewill.yxd.yunxundian.pubClass.OkHttpManger;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;


/**
 * Created by XXX on 2017/4/2.
 */
public class MainMessageFragment extends Fragment implements View.OnClickListener {

    private TextView announceMsgCount,normalMsgCount,warnMsgCount;
    private int warnCount,normalCount,announceCount;
    private Handler myHandler=new Handler();
    private Context mContext;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.main_message,container,false);
        this.mContext = getContext();
        initView(view);
        return view;
    }

    public void initView(View view){

        this.announceMsgCount= (TextView) view.findViewById(R.id.announceMsgCount);//公告
        this.normalMsgCount= (TextView) view.findViewById(R.id.normalMsgCount);//消息提醒
        this.warnMsgCount= (TextView) view.findViewById(R.id.warnMsgCount);//异常提醒

        RelativeLayout unusualNotice = (RelativeLayout) view.findViewById(R.id.unusualNotice);
        unusualNotice.setOnClickListener(this);
        RelativeLayout messageNotice = (RelativeLayout) view.findViewById(R.id.messageNotice);
        messageNotice.setOnClickListener(this);
        RelativeLayout messageAnnounce = (RelativeLayout) view.findViewById(R.id.messageAnnounce);
        messageAnnounce.setOnClickListener(this);
        /**
         * 首先获取msg的count，进入其他的activity在加载具体的消息类型
         */
        myHandler.post(netWorkChange);
    }
    Runnable netWorkChange=new Runnable() {
        @Override
        public void run() {
            if (NetWorkStateReceiver.whetherConnectNetWork==1)
                loadUserMassgeCount();
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        loadUserMassgeCount();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.unusualNotice:
                startActivityForResult(new Intent(mContext,MessageNoticeActivity.class).putExtra("label",1),1001);
                break;
            case R.id.messageNotice:
                startActivityForResult(new Intent(mContext,MessageNoticeActivity.class).putExtra("label",2),10002);
                break;
            case R.id.messageAnnounce:
                startActivityForResult(new Intent(mContext,MessageNoticeActivity.class).putExtra("label",3),10003);
                break;
        }
    }

    /*
       * 消息只能在再线加载，如果离线默认设置为0
       * */
    public void loadUserMassgeCount(){

        Map<String,String> map=new HashMap<>();
        map.put("empid",MainActivity.id+"");
        map.put("task","loadNewMsgNum");
        try {
            OkHttpManger.getInstance().postAsync(pubClass.Server_namehzm, new OkHttpManger.MyCallback() {
                @Override
                public void onSuccess(String result) {
                    //判断传过来的数据类型，得到的数据解析后设置msg的个数
                    String[] resArray=result.split("\\|");
                    if (resArray[0].equals("200")){
                        String[] resultMsg=resArray[3].split(",");
                        String[] yichang= resultMsg[0].split(":");
                        String[] xiaoxi= resultMsg[1].split(":");
                        String[] gonggao= resultMsg[2].split(":");
                        if (!yichang[1].equals("0")){
                            warnMsgCount.setVisibility(View.VISIBLE);
                            warnCount=Integer.parseInt(yichang[1]);
                            if (warnCount>99){
                                warnMsgCount.setText(String.valueOf(99));
                            }else{
                                warnMsgCount.setText(String.valueOf(warnCount));
                            }
                        }else{
                            warnMsgCount.setVisibility(View.GONE);
                        }
                        if (!xiaoxi[1].equals("0")){
                            normalMsgCount.setVisibility(View.VISIBLE);
                            normalCount=Integer.parseInt(xiaoxi[1]);
                            if (normalCount>99){
                                normalMsgCount.setText(String.valueOf(99));
                            }else{
                                normalMsgCount.setText(String.valueOf(normalCount));
                            }
                        }else{
                            normalMsgCount.setVisibility(View.GONE);
                        }
                        if (!gonggao[1].equals("0")){
                            announceMsgCount.setVisibility(View.VISIBLE);
                            announceCount=Integer.parseInt(gonggao[1]);
                            if (announceCount>99){
                                announceMsgCount.setText(String.valueOf(99));
                            }else{
                                announceMsgCount.setText(String.valueOf(normalCount));
                            }
                        }else{
                            announceMsgCount.setVisibility(View.GONE);
                        }
                    }
                }

                @Override
                public void onFailture() {
         //           Toast.makeText(mContext,"消息初始化失败",Toast.LENGTH_SHORT).show();
                }
            },map);
        } catch (IOException e) {
            e.printStackTrace();
        }
   }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1001:
                if (data!=null) {
                    int count = data.getIntExtra("count", 0);
                    warnCount=warnCount-count;
                    if (warnCount!=0)
                        if (warnCount>=99) warnMsgCount.setText(String.valueOf(99));
                        else warnMsgCount.setText(String.valueOf(warnCount));
                    else
                        warnMsgCount.setVisibility(View.GONE);
                }
                break;
            case 1002:
                if (data!=null){
                    int count = data.getIntExtra("count", 0);
                    normalCount=normalCount-count;
                    if (normalCount!=0)
                        if (normalCount>=99) normalMsgCount.setText(String.valueOf(99));
                        else  normalMsgCount.setText(String.valueOf(normalCount));
                    else
                        normalMsgCount.setVisibility(View.GONE);
                }

                break;
            case 1003:
                if (data!=null){
                    int count = data.getIntExtra("count", 0);
                    announceCount=announceCount-count;

                    if (announceCount!=0)
                        if (announceCount>99) announceMsgCount.setText(String.valueOf(99));
                        else  announceMsgCount.setText(String.valueOf(announceCount));
                    else
                        announceMsgCount.setVisibility(View.GONE);
                }
                break;
        }

    }
}
